package example

import (
	"fmt"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/widget"
	"github.com/tomcraven/gotable"
)

func Gotable(a fyne.App) {
	w := a.NewWindow("")
	w.Resize(fyne.NewSize(900, 600))
	result := widget.NewMultiLineEntry()
	msg := widget.NewLabel("")

	btn := widget.NewButton("do", func() {
		t := gotable.NewTable([]gotable.Column{
			gotable.NewColumn("column 1", 11),
			gotable.NewColumn("column 2", 20),
		})

		t.Push(23, 42)
		t.Push("hello", "world")
		t.Push(true, false)
		t.Push(123456789987654321, "this line is too long")
		t.Push(23.42, 42.23)
		t.Push(&customCell{}, &customCell{})
		t.Push("blank right", nil)
		t.Push(nil, "blank left")
		t.PrintTo(newOutput(result))
		t.Print()
	})
	c := container.NewBorder(btn, msg, nil, nil, result)
	w.SetContent(c)
	w.CenterOnScreen()
	w.Show()
}

type customCell struct {
}

func (c *customCell) Print(output gotable.Output) {
	output.Print("custom cell")
}

func newOutput(result *widget.Entry) *OutputStdOut {
	return &OutputStdOut{result: result}
}

// OutputStdOut prints all messages to std out
type OutputStdOut struct {
	result *widget.Entry
}

// Print - prints to std out
func (o *OutputStdOut) Print(a string) gotable.Output {
	o.result.SetText(o.result.Text + fmt.Sprintln(a))
	// fmt.Println(a)
	return o
}

// Flush - noop
func (o *OutputStdOut) Flush() gotable.Output {
	return o
}
